package com.bi.bitest.bao.widgets;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.bi.bitest.bao.R;
import com.bi.bitest.bao.utils.DimensionUtil;

/**
 * Created
 * Author:bi[FR]
 * Date:15-10-23
 */
public class ColumnsItemView extends RelativeLayout {
    private ImageView imgIcon;//听单的图标
    private TextView txtTitle;
    private TextView txtSubTitle;  //听单子标题  就是描述
    private ImageView imgArrow;   //右侧箭头
    private ImageView imgLine;
    /**
     * 在代码中使用SpecialItemView的时候调用
     *
     * @param context
     */
    public ColumnsItemView(Context context) {
        this(context, null);
    }
    /**
     * xml中使用控件的时候调用
     * @param context
     * @param attrs
     */
    public ColumnsItemView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context, attrs);
    }
    private void init(Context context, AttributeSet attrs){
        //初始化控件
        imgIcon=new ImageView(context);
        //手写属性  LayoutOarams 控件要添加到哪一个容器中就用那一个容器的layoutParams
        RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(DimensionUtil.dp2px(context, 60)
                ,DimensionUtil.dp2px(context,60));//这些单位 java中都是px 像素
        lp.addRule(CENTER_VERTICAL);
        lp.leftMargin=DimensionUtil.dp2px(context,10);
        imgIcon.setLayoutParams(lp);
        imgIcon.setImageResource(R.mipmap.ic_launcher);
        //设置ID
        imgIcon.setId(R.id.columns_item_icon);
        addView(imgIcon);

        txtTitle=new TextView(context);
        lp=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
                , ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.addRule(ALIGN_TOP,R.id.columns_item_icon);
        lp.addRule(RIGHT_OF, R.id.columns_item_icon);
        lp.rightMargin=DimensionUtil.dp2px(context,20);
        lp.leftMargin=DimensionUtil.dp2px(context, 10);//单位像素  需要机型适配

        txtTitle.setLayoutParams(lp);
        txtTitle.setText("标题");
        txtTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 18);
        txtTitle.setTextColor(Color.BLACK);
        txtTitle.setId(R.id.columns_item_title);
        txtTitle.setSingleLine();//单行
        addView(txtTitle);
//subtitle
        txtSubTitle=new TextView(context);
        lp=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
                , ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.addRule(BELOW,R.id.columns_item_title);
        lp.addRule(ALIGN_LEFT, R.id.columns_item_title);
        lp.rightMargin=DimensionUtil.dp2px(context,20);

        txtSubTitle.setLayoutParams(lp);
        txtSubTitle.setText("Subtitle");
        txtSubTitle.setTextSize(TypedValue.COMPLEX_UNIT_SP, 13);
        txtSubTitle.setId(R.id.columns_item_subtitle);
        txtSubTitle.setEllipsize(TextUtils.TruncateAt.valueOf("END"));
        txtSubTitle.setSingleLine();//单行
        addView(txtSubTitle);


        //-----------imgArrow
        imgArrow=new ImageView(context);
        lp=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT
                , ViewGroup.LayoutParams.WRAP_CONTENT);
        imgArrow.setImageResource(R.drawable.arrow_right);
        lp.addRule(ALIGN_PARENT_RIGHT);
        int left = DimensionUtil.dp2px(context, 5); // 5dp
        lp.leftMargin=left;
        lp.rightMargin = DimensionUtil.dp2px(context,10);
        lp.addRule(CENTER_VERTICAL);
        imgArrow.setLayoutParams(lp);
        addView(imgArrow);
        //-----------
        imgLine=new ImageView(context);
        lp=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,1);/////////////////////
        lp.addRule(ALIGN_LEFT,R.id.columns_item_title);
        lp.addRule(ALIGN_PARENT_RIGHT);
        lp.addRule(BELOW,R.id.columns_item_subtitle);
        //左上右下间距
        int topMargin = DimensionUtil.dp2px(context, 10); // 5dp
        lp.setMargins(0, topMargin, 0, 0);
        imgLine.setLayoutParams(lp);
        imgLine.setImageResource(R.drawable.shape_line);
        addView(imgLine);
    }

    public void setTitle(String str){
        txtTitle.setText(str);
    }
    public void setSubTtile(String subtitle){
        txtSubTitle.setText(subtitle);
    }
    public void setShowBottomLine(Boolean show){
        if(show){
            imgLine.setVisibility(VISIBLE);
        }else   imgLine.setVisibility(INVISIBLE);
    }
    public void setRightArrowResource(int resId){
        imgArrow.setImageResource(resId);
    }
    public ImageView getImgIcon(){
        return imgIcon;
    }

}
